package com.example.chenrukang.my_study_1_kuangjia;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.chenrukang.my_study_1_kuangjia.bean.Tab;
import com.example.chenrukang.my_study_1_kuangjia.fragmet.HomeFragment;
import com.example.chenrukang.my_study_1_kuangjia.widget.FragmentTabHost;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private LayoutInflater mLayoutInflater;
    private FragmentTabHost mFragmentTabHost;
    private List<Tab> mTabs = new ArrayList<>(5);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTab();

    }

    private void initTab() {
        Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.drawable.selector_icon_home);
        Tab tab_hot = new Tab(HomeFragment.class,R.string.hot,R.drawable.selector_icon_hot);
        Tab tab_category = new Tab(HomeFragment.class,R.string.catagory,R.drawable.selector_icon_category);
        Tab tab_cart = new Tab(HomeFragment.class,R.string.cart,R.drawable.selector_icon_cart);
        Tab tab_mine = new Tab(HomeFragment.class,R.string.mine,R.drawable.selector_icon_mine);

        mTabs.add(tab_home);
        mTabs.add(tab_hot);
        mTabs.add(tab_category);
        mTabs.add(tab_cart);
        mTabs.add(tab_mine);

        mLayoutInflater = LayoutInflater.from(this);
        mFragmentTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

        for(Tab tab : mTabs){
            TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(getString(tab.getTitle()));
            tabSpec.setIndicator(buildIndicator(tab));
            mFragmentTabHost.addTab(tabSpec,tab.getFragment(),null);

        }

        mFragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mFragmentTabHost.setCurrentTab(0);

    }

    private View buildIndicator(Tab tab) {
        View view= mLayoutInflater.inflate(R.layout.tab_indicator,null);

        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView text = (TextView) view.findViewById(R.id.txt_indicator);

        img.setBackgroundResource(tab.getIcon());

        text.setText(tab.getTitle());

        return view;

    }


}
